<?php 
    session_start();
    include_once 'inc/db.class.php';
    include_once 'inc/function.php';
    include_once 'sesslon.php';
    $oid=isset($_POST["oid"]) ? $_POST["oid"] : "";
    if($oid==""){
        //跳转到一个漂亮的页面提示系统繁忙
        exit("sorry 系统繁忙");
    }
        //读取该用户收货地址
    $sqla="select * from address where mid=".getmid($_SESSION["jjusername"]);
    $rsa=$db->getData($sqla);
    //产品
    $sqlb="select basket.*,business.name from basket,business where basket.bid=business.id and mid=".getmid($_SESSION["jjusername"])." and flag=0";
    $rsb=$db->getData($sqlb);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<?php include_once 'meta.php'; ?>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
		<link href="assets/css/amazeui.css" rel="stylesheet" type="text/css" />

		<link href="basic/css/demo.css" rel="stylesheet" type="text/css" />
		<link href="css/cartstyle.css" rel="stylesheet" type="text/css" />

		<link href="css/jsstyle.css" rel="stylesheet" type="text/css" />

		<script type="text/javascript" src="js/address.js"></script>
		<script type="text/javascript" src="js/jquery-3.2.1.js"></script>
        <script type="text/javascript" src="layer/layer.js"></script>
        <script src="distpicker/distpicker.data.js"></script>
	    <script src="distpicker/distpicker.js"></script>
    	<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script>
    	<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
    	<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>
    	<script src="layer/layer.js"></script>
    	<script src="js/jquery.serializejson.js"></script>

        <script>
			function setdefault(id,o){
					$.ajax({
						url:"ajax/setdefault.php",
						type:"post",
						data:{"id":id},
						success:function(data){
							if(data==0){
								layer.msg("请登录后操作",{icon:5});
							}else if(data==1){
								layer.msg("数据非法",{icon:5});
							}else if(data==2){
								layer.msg("设为默认地址",{icon:6});
								//让其他元素去掉class=hidden
								$(".setdef").removeClass("hidden")
								//让设为默认的按钮消失
								$(o).addClass("hidden");
								//默认地址文字显示
								var m=$(".setdef").index($(o));
								$(".address-left ins").addClass("hidden");
								$(".address-left ins").eq(m).removeClass("hidden");
								
							}else if(data==3){
								layer.msg("修改失败1 ",{icon:5});
							}else{
								layer.msg("修改失败 2",{icon:5});
							}
						}

					});
				}
			$(document).ready(function(){
				$(".user-addresslist").click(function(){
					//下面的框子里显示你选中的收获地址
					fun2($(this));
				});

				//公共方法
				fun2=function(o){
					$("#myprovince").text(o.find(".province").text());
					$("#mycity").text(o.find(".city").text());
					$("#myarea").text(o.find(".dist").text());
					$("#myaddress").text(o.find(".street").text());
					$("#myname").text(o.find(".buy-user").text());
					$("#mytel").text(o.find(".buy-phone").text());
					$("#addressid").val(o.find(".addresslistid").val());
				}
				
			});
			var fun2;
			$.validator.setDefaults({
				submitHandler:function(){
					//执行ajax提交数据
					//alert(JSON.stringify($("#formaddress").serializeJSON()));
					$.ajax({
						url:"ajax/addaddress.php",
						type:"post",
						data:$("#formaddress").serializeJSON(),
						dataType:"json",
						success:function(data){
							//alert(data);
							if(data==0){
								layer.msg("请登录后操作",{icon:5});
							}else if(data==1){
								//关闭本窗口
								layer.msg("新增成功",{icon:6});

								$(document.body).css("overflow","visible");
								$('.theme-login').removeClass("selected");
								$('.item-props-can').removeClass("selected");					
								$('.theme-popover-mask').hide();
								$('.theme-popover').slideUp(200);	

								// 显示新增收货地址
								var d=$("#formaddress").serializeJSON();
								var stradd=$(".jqaddress").html();
								stradd=stradd.replace(/\{name\}/g,d.name);
								stradd=stradd.replace(/\{tel\}/g,d.tel);
								stradd=stradd.replace(/\{province\}/g,d.province);
								stradd=stradd.replace(/\{city\}/g,d.city);
								stradd=stradd.replace(/\{area\}/g,d.area);
								stradd=stradd.replace(/\{address\}/g,d.address);
								stradd=stradd.replace(/\{addresslistid\}/g,data['id']);
								//alert(stradd);
								$(".myaddresslist").append(stradd);
								$(".user-addresslist").last().bind("click",function(){
									$(this).addClass("defaultAddr").siblings().removeClass("defaultAddr");
									fun2($(this));
								});
								$(".user-addresslist").last().click();
								
							}else if(data==2){
								layer.msg("添加失败2",{icon:5});
							}else{
								layer.msg("添加失败3",{icon:5});
							}
						},
						error:function(){
							layer.msg("添加失败1",{icon:5});
						}
					});
				}
			});
			$(document).ready(function(){
				$("#formaddress").validate({
					rules:{
						name:{
							required:true
						},
						tel:{
							required:true,
							cktel:true
						},
						province:{
							required:true,
							
						},
						city:{
							required:true,
							
						},
						area:{
							required:true,
							
						},
						address:{
							required:true,
							
						},
						
					},
					messages:{
						name:"请填写收货人姓名",
						tel:"请填写收货人手机号",
						province:"请填写收货省份",
						city:"请填写手收货城市",
						area:"请填写区域",
						address:"请填写收货详细地址"
					}
				});

				$.validator.addMethod("cktel",function(value){
					var pre = /^1[3456789]\d{9}$/;        
					return pre.test(value);},"*请输入正确的手机号！");
			});


			//删除收货地址
			function deladdress(id,o){
				layer.confirm("确定是否删除该收货地址",{btn:["确定","再想想"]},function(){
					$.ajax({
						url:"ajax/deladdress.php",
						type:"post",
						data:{"id":id},
						success:function(data){
							if(data==0){
								layer.msg("请登录后操作",{icon:5});
							}else if(data==1){
								layer.msg("数据非法",{icon:5});
							}else if(data==2){
								layer.msg("删除成功",{icon:6});
								//判断当前删除的Li 和下面的确认信息是否有关联
								//alert(id+" "+$(".box").find("#addressid").val());
								if(id==$(".box").find("#addressid").val()){
									
									//把确认信息里面的所有信息都更改成默认信息
									//找到当前地址li里面哪一个是 默认地址的Li
									for(var v=0;v<$(".user-addresslist").length;v++){
										if($(".user-addresslist").eq(v).find("ins").css("display")!="none"){
											var p1=$(".user-addresslist").eq(v).find(".province").text();
											var c1=$(".user-addresslist").eq(v).find(".city").text();
											var a1=$(".user-addresslist").eq(v).find(".dist").text();
											var add1=$(".user-addresslist").eq(v).find(".street").text();
											var n1=$(".user-addresslist").eq(v).find(".buy-user").text();
											var t1=$(".user-addresslist").eq(v).find(".buy-phone").text();
											var i1=$(".user-addresslist").eq(v).find(".addresslistid").val();
											break;
										}
									}
									$("#myprovince").text(p1);
									$("#mycity").text(c1);
									$("#myarea").text(a1);
									$("#myaddress").text(add1);
									$("#myname").text(n1);
									$("#mytel").text(t1);
									$("#addressid").val(i1);
								}
								//移除这个li
								$(o).parent().parent().remove();
							}else if(data==3){
								layer.msg("删除失败2",{icon:5});
							}else if(data==4){
								layer.msg("默认收货地址不允许删除",{icon:5});
							}else{
								layer.msg("删除失败1",{icon:5});
							}
						}
					});
				},function(){});
				//alert(event)
				event.stopPropagation();
			}
        </script>
        
	</head>

	<body>

		<?php include_once 'header1.php'; ?>

			<div class="clear"></div>
			<div class="concent">
				<!--地址 -->
				<div class="paycont">
					<div class="address">
						<h3>确认收货地址 </h3>
						<div class="control">
							<div class="tc-btn createAddr theme-login am-btn am-btn-danger">使用新地址</div>
						</div>
						<div class="clear"></div>
						<ul class="myaddresslist">
						    <?php foreach ($rsa as $v){ ?>
							<div class="per-border"></div>
							<li class="user-addresslist <?php if($v["flag"]==1){echo "defaultAddr";} ?>">

								<div class="address-left">
									<div class="user DefaultAddr">

										<span class="buy-address-detail">   
                   <span class="buy-user"><?php echo $v["name"]; ?> </span>
										<span class="buy-phone"><?php echo $v["tel"]; ?></span>
										</span>
									</div>
									<div class="default-address DefaultAddr">
										<span class="buy-line-title buy-line-title-type">收货地址：</span>
										<span class="buy--address-detail">
								        <span class="province"><?php echo $v["province"]; ?></span>省
										<span class="city"><?php echo $v["city"]; ?></span>
										<span class="dist"><?php echo $v["area"]; ?></span>
										<span class="street"><?php echo $v["address"]; ?></span>
										</span>

										</span>
									</div>
									
									<ins class="deftip <?php if($v["flag"]==0){echo "hidden";}?>">默认地址</ins>
									
								</div>
								<div class="address-right">
									<a href="person/address.html">
										<span class="am-icon-angle-right am-icon-lg"></span></a>
								</div>
								<div class="clear"></div>

								<div class="new-addr-btn">
									<a href="javascript:;" onclick="setdefault(<?php echo $v["id"]; ?>,this)" class="setdef <?php if($v["flag"]==1){echo "hidden";} ?>">设为默认</a>
									<span class="new-addr-bar hidden">|</span>
									<a href="#">编辑</a>
									<span class="new-addr-bar">|</span>
									<a href="javascript:void(0);" onclick="deladdress(<?php echo $v["id"] ?>,this)">删除</a>
								</div>
                                 
							</li>
							<?php } ?>
						</ul> 
						 <!-- jq添加收获地址 -->
				            <div class="jqaddress hidden">
    				            <div class="per-border"></div>
    							<li class="user-addresslist ">
    
    								<div class="address-left">
    									<div class="user DefaultAddr">
    
    										<span class="buy-address-detail">   
                       <span class="buy-user">{name}</span>
    										<span class="buy-phone">{tel}</span>
    										</span>
    									</div>
    									<div class="default-address DefaultAddr">
    										<span class="buy-line-title buy-line-title-type">收货地址：</span>
    										<span class="buy--address-detail">
    								        <span class="province">{province}</span>
    										<span class="city">{city}</span>
    										<span class="dist">{area}</span>
    										<span class="street">{address}</span>
    										</span>
    
    										</span>
    									</div>
    									
    									
    								</div>
    								<div class="address-right">
    									<a href="person/address.html">
    										<span class="am-icon-angle-right am-icon-lg"></span></a>
    								</div>
    								<div class="clear"></div>
    
    								<div class="new-addr-btn">
    									<a href="javascript:;" onclick="setdefault({addresslistid},this)" class="setdef <?php if($v["flag"]==1){echo "hidden";} ?>">设为默认</a>
    									<span class="new-addr-bar hidden">|</span>
    									<a href="#">编辑</a>
    									<span class="new-addr-bar">|</span>
    									<a href="javascript:void(0);" onclick="deladdress({addresslistid},this);">删除</a>
    								</div>
                                    <input type="text" class="addresslistid" value="{addresslistid}" />
    							</li>
    						</div>
				            <!-- end -->
						</ul>
						
						<div class="clear"></div>
					</div>
					
					<div class="clear"></div>

					<!--支付方式-->
					<div class="logistics">
						<h3>选择支付方式</h3>
						<ul class="pay-list">
							<li class="pay card"><img src="images/wangyin.jpg" />银联<span></span></li>
							<li class="pay qq"><img src="images/weizhifu.jpg" />微信<span></span></li>
							<li class="pay taobao"><img src="images/zhifubao.jpg" />支付宝<span></span></li>
						</ul>
					</div>
					<div class="clear"></div>

					<!--订单 -->
					<div class="concent">
						<div id="payTable">
							<h3>确认订单信息</h3>
							<div class="cart-table-th">
								<div class="wp">

									<div class="th th-item">
										<div class="td-inner">商品信息</div>
									</div>
									<div class="th th-price">
										<div class="td-inner">单价</div>
									</div>
									<div class="th th-amount">
										<div class="td-inner">数量</div>
									</div>
									<div class="th th-sum">
										<div class="td-inner">金额</div>
									</div>
									<div class="th th-oplist">
										<div class="td-inner">配送方式</div>
									</div>

								</div>
							</div>
							<div class="clear"></div>
                             <?php foreach ($rsb as $v){ ?>
							<tr class="item-list">
								<div class="bundle  bundle-last">

									<div class="bundle-main">
										<ul class="item-content clearfix">
											<div class="pay-phone">
												<li class="td td-item">
													<div class="item-pic">
														<a href="#" class="J_MakePoint">
															<img src="images/kouhong.jpg_80x80.jpg" class="itempic J_ItemImg"></a>
													</div>
													<div class="item-info">
														<div class="item-basic-info">
															<a href="introduction.php?id=<?php echo $v["gid"]; ?>" class="item-title J_MakePoint" data-point="tbcart.8.11"><?php echo $v["gname"] ?></a>
														</div>
													</div>
												</li>
												<li class="td td-info">
													<div class="item-props">
														<span class="sku-line">颜色：12#川南玛瑙</span>
														<span class="sku-line">包装：裸装</span>
													</div>
												</li>
												<li class="td td-price">
													<div class="item-price price-promo-promo">
														<div class="price-content">
															<em class="J_Price price-now">39.00</em>
														</div>
													</div>
												</li>
											</div>
											<li class="td td-amount">
												<div class="amount-wrapper ">
													<div class="item-amount ">
														<span class="phone-title">购买数量</span>
														<div class="sl">
															<input class="min am-btn" name="" type="button" value="-" />
															<input class="text_box" name="" type="text" value="3" style="width:30px;" />
															<input class="add am-btn" name="" type="button" value="+" />
														</div>
													</div>
												</div>
											</li>
											<li class="td td-sum">
												<div class="td-inner">
													<em tabindex="0" class="J_ItemSum number">117.00</em>
												</div>
											</li>
											<li class="td td-oplist">
												<div class="td-inner">
													<span class="phone-title">配送方式</span>
													<div class="pay-logis">
														快递<b class="sys_item_freprice">10</b>元
													</div>
												</div>
											</li>

										</ul>
										<div class="clear"></div>

									</div>
							</tr>
							<?php } ?>
							<div class="clear"></div>
							</div>

							
							</div>
							<div class="clear"></div>
							<div class="pay-total">
						<!--留言-->
							<div class="order-extra">
								<div class="order-user-info">
									<div id="holyshit257" class="memo">
										<label>买家留言：</label>
										<input type="text" title="选填,对本次交易的说明（建议填写已经和卖家达成一致的说明）" placeholder="选填,建议填写和卖家达成一致的说明" class="memo-input J_MakePoint c2c-text-default memo-close">
										<div class="msg hidden J-msg">
											<p class="error">最多输入500个字符</p>
										</div>
									</div>
								</div>

							</div>
							<!--优惠券 -->
							<div class="buy-agio">
								<li class="td td-coupon">

									<span class="coupon-title">优惠券</span>
									<select data-am-selected>
										<option value="a">
											<div class="c-price">
												<strong>￥8</strong>
											</div>
											<div class="c-limit">
												【消费满95元可用】
											</div>
										</option>
										<option value="b" selected>
											<div class="c-price">
												<strong>￥3</strong>
											</div>
											<div class="c-limit">
												【无使用门槛】
											</div>
										</option>
									</select>
								</li>

								<li class="td td-bonus">

									<span class="bonus-title">红包</span>
									<select data-am-selected>
										<option value="a">
											<div class="item-info">
												¥50.00<span>元</span>
											</div>
											<div class="item-remainderprice">
												<span>还剩</span>10.40<span>元</span>
											</div>
										</option>
										<option value="b" selected>
											<div class="item-info">
												¥50.00<span>元</span>
											</div>
											<div class="item-remainderprice">
												<span>还剩</span>50.00<span>元</span>
											</div>
										</option>
									</select>

								</li>

							</div>
							<div class="clear"></div>
							</div>
							<!--含运费小计 -->
							<div class="buy-point-discharge ">
								<p class="price g_price ">
									合计（含运费） <span>¥</span><em class="pay-sum">244.00</em>
								</p>
							</div>

							<!--信息 -->
							<div class="order-go clearfix">
								<div class="pay-confirm clearfix">
									<div class="box">
										<div tabindex="0" id="holyshit267" class="realPay"><em class="t">实付款：</em>
											<span class="price g_price ">
                                    <span>¥</span> <em class="style-large-bold-red " id="J_ActualFee">244.00</em>
											</span>
										</div>
                                        <?php 
                                            foreach ($rsa as $v){
                                                if($v["flag"]==0){
                                                    continue;
                                                }
                                        ?>
										<div id="holyshit268" class="pay-address">

											<p class="buy-footer-address">
												<span class="buy-line-title buy-line-title-type">寄送至：</span>
												<span class="buy--address-detail">
								   <span id="myprovince" class="province"><?php echo $v["province"]; ?></span>
												<span id="mycity" class="city"><?php echo $v["city"]; ?></span>
												<span id="myarea" class="dist"><?php echo $v["area"]; ?></span>
												<span id="myaddress" class="street"><?php echo $v["address"]; ?></span>
												</span>
												</span>
											</p>
											<p class="buy-footer-address">
												<span class="buy-line-title">收货人：</span>
												<span class="buy-address-detail">   
                                         <span id="myname" class="buy-user"><?php echo $v["name"]; ?> </span>
												<span id="mytel" class="buy-phone"><?php echo $v["tel"]; ?></span>
												</span>
											</p>
										</div>
										<?php } ?>
									</div>

									<div id="holyshit269" class="submitOrder">
										<div class="go-btn-wrap">
											<a id="J_Go" href="success.php" class="btn-go" tabindex="0" title="点击此按钮，提交订单">提交订单</a>
										</div>
									</div>
									<div class="clear"></div>
								</div>
							</div>
						</div>

						<div class="clear"></div>
					</div>
				</div>
				<?php include_once 'footer.php'; ?>
			</div>
			<div class="theme-popover-mask"></div>
			<div class="theme-popover">

				<!--标题 -->
				<div class="am-cf am-padding">
					<div class="am-fl am-cf"><strong class="am-text-danger am-text-lg">新增地址</strong> / <small>Add address</small></div>
				</div>
				<hr/>

				<div class="am-u-md-12">
					<form id="formaddress" class="am-form am-form-horizontal">

						<div class="am-form-group">
							<label for="user_name" class="am-form-label">收货人</label>
							<div class="am-form-content">
								<input type="text" id="user_name" name="name" placeholder="收货人">
							</div>
						</div>

						<div class="am-form-group">
							<label for="user_phone" class="am-form-label">手机号码</label>
							<div class="am-form-content">
								<input type="text" id="user_phone" name="tel" placeholder="手机号必填" >
							</div>
						</div>

						<div class="am-form-group">
							<label for="user_phone" class="am-form-label">所在地</label>
							<div class="am-form-content address">
        								<div data-toggle="distpicker">
                                           <div class="form-group">
                                              <label class="sr-only" for="province1">Province</label>
                                              <select class="form-control" name="province" id="province1"></select>
                                            </div>
                                            <div class="form-group">
                                              <label class="sr-only" for="city1">City</label>
                                              <select class="form-control" name="city" id="city1"></select>
                                            </div>
                                            <div class="form-group">
                                              <label class="sr-only" for="district1">District</label>
                                              <select class="form-control" name="area" id="district1"></select>
                                            </div>
                                        </div>
        							</div>
						</div>

						<div class="am-form-group">
							<label for="user_intro" class="am-form-label">详细地址</label>
							<div class="am-form-content">
								<textarea class="" rows="3" id="user_intro" name="address" placeholder="输入详细地址"></textarea>
								<small>100字以内写出你的详细地址...</small>
							</div>
						</div>

						<div class="am-form-group theme-poptit">
							<div class="am-u-sm-9 am-u-sm-push-3">
								<input class="am-btn am-btn-danger" type="submit" value="保存" />
								<div class="am-btn am-btn-danger close">取消</div>
							</div>
						</div>
					</form>
				</div>

			</div>

			<div class="clear"></div>
	</body>

</html>